
Ext.namespace("Ext.ux.workholiday");

/**
 * 工作时间设定
 * @class Ext.ux.workholiday.workTime
 * @extends Ext.Container
 */
Ext.ux.workholiday.WorkTime = Ext.extend(Ext.Container,{
	dayText: '',
	dayOfWeek: '',
	parentPanel: null,
	
	initComponent: function(){
		this.items = [{
					layout: 'column',
					items:[{
						columnWidth: .1,
						layout: 'form',
						items: [{
							xtype: 'label',
							text: this.dayText
						},{
							xtype: 'textfield',
							hidden: true,
							name: 'dayOfWeeks',
							value: this.dayOfWeek
						}]
					},{
						columnWidth: .7,
						layout: 'column',
						items: [{
							columnWidth: .5,
							items: [{
								layout: 'column',
								items: [{
									columnWidth: .20,
									layout: 'form',
									items: [{
										xtype: 'label',
										text: '上午从:'
									}]
								},{
									columnWidth: .25,
									items: [
										new Ext.ux.workholiday.TimeComboBox({
											width: 60,
											amOrPm: 'am',
											startOrEnd: 'start',
											hiddenName:'amStartTimes',
											name: 'amStartTimes',
											id: 'amStartTimes' + this.dayOfWeek,
											anchor: '95%'
										})
									]
								},{
									columnWidth: .10,
									layout: 'form',
									items: [{
										xtype: 'label',
										width: 15
									}]
							},{
									columnWidth: .10,
									layout: 'form',
									items: [{
										xtype: 'label',
										text: '到'
									}]
							},{
									columnWidth: .25,
									items: [
										new Ext.ux.workholiday.TimeComboBox({
											width: 60,
											amOrPm: 'am',
											startOrEnd: 'end',
											hiddenName:'amEndTimes',
											name: 'amEndTimes',
											id: 'amEndTimes' + this.dayOfWeek,
											anchor: '95%'
										})
									]
							}]
						}]
					},{
						columnWidth: .5,
						items: [{
							layout: 'column',
							items: [{
								columnWidth: .20,
								layout: 'form',
								items: [{
									xtype: 'label',
									text: '下午从：'
								}]
							},{
								columnWidth: .25,
								items: [
									new Ext.ux.workholiday.TimeComboBox({
										width: 60,
										amOrPm: 'pm',
										startOrEnd: 'start',
										hiddenName:'pmStartTimes',
										name: 'pmStartTimes',
										id: 'pmStartTimes' + this.dayOfWeek,
										anchor: '95%'
									})
								]
							},{
								columnWidth: .10,
								layout: 'form',
								items: [{
									xtype: 'label',
									width: 15
								}]
							},{
								columnWidth: .10,
								layout: 'form',
								items: [{
									xtype: 'label',
									text: '到'
								}]
							},{
								columnWidth: .25,
								items: [
									new Ext.ux.workholiday.TimeComboBox({
										width: 60,
										amOrPm: 'pm',
										startOrEnd: 'end',
										hiddenName:'pmEndTimes',
										name: 'pmEndTimes',
										id: 'pmEndTimes' + this.dayOfWeek,
										anchor: '95%'
									})
								]
							}]
						}]
					}
					]
					
					},{
					columnWidth: .2,
					layout: 'form',
					items: [{
						xtype: 'button',
						text: '应用到所有工作日',
						handler: this.applyToAllDay,
						scope: this
					}]
					
				}]
		}];
		Ext.ux.workholiday.WorkTime.superclass.initComponent.call(this);
	},
	applyToAllDay: function(){
		var dayOfWeek = this.dayOfWeek;
		this.parentPanel.applyToAllDay(dayOfWeek);
	}
});


/**
 * 工作时间设定
 */
Ext.ux.workholiday.WorkTimeSetPanel = Ext.extend(Ext.ux.Wiz.Card, {
 	wizardWin: null,
 	monitorValid: true, 
	labelAlign: 'right',
	labelWidth: 20,
	frame:true,
	timeSetItems: null,
	initComponent: function(){
		Ext.ux.workholiday.WorkTimeSetPanel.superclass.initComponent.call(this);
	},
	//显示选项卡时，动态添加工作日
	onCardShow:	function(){
		this.removeAll();
		
		var workDays = this.wizardWin.getWorkDays().join();
		var days = ['Sunday', 'Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday'];
		var mathedDays = ['星期日', '星期一', '星期二', '星期三', '星期四', '星期五', '星期六'];
		var items = new Array();
		for(var i = 0; i < days.length; i++){
			if(workDays.indexOf(days[i]) >= 0){
				items[items.length] = new Ext.ux.workholiday.WorkTime({
					dayText: mathedDays[i],
					dayOfWeek : i+1,
					parentPanel: this
				});
			}
		}
		this.timeSetItems = items;
		this.add(items);
		this.doLayout();
	},
	//判断
	isValid: function(){
		var len = this.timeSetItems.length;
		
		for(var i = 0; i < len; i++){
			var amStartTime = this.form.findField('amStartTimes' + this.timeSetItems[i].dayOfWeek).getValue();
			var amEndTime = this.form.findField('amEndTimes' + this.timeSetItems[i].dayOfWeek).getValue();
			
			var pmStartTime = this.form.findField('pmStartTimes' + this.timeSetItems[i].dayOfWeek).getValue();
			var pmEndTime = this.form.findField('pmEndTimes' + this.timeSetItems[i].dayOfWeek).getValue();
			
			if( this.compareTo(amStartTime, amEndTime) > 0){
			   return false;
			}
			if( this.compareTo(pmStartTime, pmEndTime) > 0){
				return false;
			}
		}
		return true;
	},
	compareTo: function(time1, time2){
		var time1 = Date.parseDate(time1, "G:i");
		var time2 = Date.parseDate(time2, "G:i")
		if(time1 > time2) {
			return 1;
		}
		if(time1 < time2){
			return -1;
		}
		return 0;
	},
	applyToAllDay: function(dayOfWeek){
		var amStartTime = Ext.getCmp('amStartTimes' + dayOfWeek).getValue();
		var amEndTime = this.form.findField('amEndTimes' + dayOfWeek).getValue();
		var pmStartTime = this.form.findField('pmStartTimes' + dayOfWeek).getValue();
		var pmEndTime = this.form.findField('pmEndTimes' + dayOfWeek).getValue();
		
		var len = this.timeSetItems.length;
		
		for(var i = 0; i < len; i++){
			if(this.timeSetItems[i].dayOfWeek != dayOfWeek){
				this.form.findField('amStartTimes' + this.timeSetItems[i].dayOfWeek).setValue(amStartTime);
				this.form.findField('amEndTimes' + this.timeSetItems[i].dayOfWeek).setValue(amEndTime);
				this.form.findField('pmStartTimes' + this.timeSetItems[i].dayOfWeek).setValue(pmStartTime);
				this.form.findField('pmEndTimes' + this.timeSetItems[i].dayOfWeek).setValue(pmEndTime);
				
				this.form.findField('amStartTimes' + this.timeSetItems[i].dayOfWeek).setRawValue(amStartTime);
				this.form.findField('amEndTimes' + this.timeSetItems[i].dayOfWeek).setRawValue(amEndTime);
				this.form.findField('pmStartTimes' + this.timeSetItems[i].dayOfWeek).setRawValue(pmStartTime);
				this.form.findField('pmEndTimes' + this.timeSetItems[i].dayOfWeek).setRawValue(pmEndTime);
			}
		}
	}
	
 });